package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    private static final String path = "http://localhost/img/images/";

    @Autowired
    AliOssUtil aliOssUtil;

//    @Resource
//    private MinioClient minioClient;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("上传文件:{}",file);
        try {
            //原始文件名
            String filename = file.getOriginalFilename();
            //截取原始文件名后缀
            String extension = filename.substring(filename.lastIndexOf("."));
            //文件名拼接
            String objectName = UUID.randomUUID().toString() + extension;
            log.info(objectName);

            //todo 这部分是阿里云的文件上传功能  文件请求路径
//            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
//            return Result.success(filePath);
            
            //本地存储
            file.transferTo(new File("D:\\environment\\nginx-1.20.2\\html\\sky\\img\\images\\"+objectName));
            String fillPath = path+objectName;
            log.info("文件上传成功,文件请求路径:{}",fillPath);
            return Result.success(fillPath);


        } catch (IOException e) {
            log.info("文件上传失败:{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }


}
